<?php
@session_start();
//use for showing  staffs' data
//include staffs' main inferface;the model of modify;the model of delete;the model of view tasks 
    if(isset($_GET['username']) && isset($_GET['type'])){
         $username=$_GET['username'];
         $type=$_GET['type'];
         model($username,$type);
    }
    else if(isset($_GET['id'])){
        $id=$_GET['id'];
        if($id==0){
            $sql="SELECT * FROM staff WHERE idposition is not null";
            getinfor($sql);
        }
        else if($id==1){
            $sql="SELECT * FROM staff WHERE idposition='1'";
            getinfor($sql);
        }
        else if($id==2){
            $sql="SELECT * FROM staff WHERE idposition='2'";
            getinfor($sql);
        }
        else if($id==3){
            $sql="SELECT * FROM staff WHERE idposition='3'";
            getinfor($sql);
        }
    }
    if(isset($_POST['type'])){
        $type=$_POST['type'];
        if($type=="edit_infor_show"){
            edit_infor_show();
        }else if($type=="update_pinfor"){
            $firstname=$_POST['firstname'];
            $lastname=$_POST['lastname'];
            $title=$_POST['title'];
            $email=$_POST['email'];
            $phoneNo=$_POST['phoneNo'];
            update_pinfor($firstname,$lastname,$title,$email,$phoneNo);
        }else if($type=="delete_staff"){
            $staffname=$_POST['staffname'];
            deletestaff($staffname);
        }
    }
    function deletestaff($staffname){
        include 'sqlconnect.php';
        $sql="UPDATE staff SET firstName=NULL,lastName=NULL,title=NULL,email=NULL,idposition=NULL,phoneNo=NULL,totalWLUs=NULL 
        WHERE username='$staffname'";
        mysqli_query($conn,$sql);
        mysqli_close($conn);
    }
    function update_pinfor($firstname,$lastname,$title,$email,$phoneNo){
        include "sqlconnect.php";
        $staffname=$_SESSION['username'];
        $sql="UPDATE staff 
        SET firstName='$firstname',lastName='$lastname',title='$title',email='$email',phoneNo=$phoneNo 
        WHERE username='$staffname'";
        mysqli_query($conn,$sql);
        mysqli_close($conn);
        personal_infor_show();
    }
    function edit_infor_show(){
        include "sqlconnect.php";
        $staffname=$_SESSION['username'];
        $sql="SELECT * FROM staff WHERE username='$staffname'";
        $result=mysqli_query($conn,$sql);
        while ($row=mysqli_fetch_array($result)){
            $firstname=$row['firstName'];
            $lastname=$row['lastName'];
            $title=$row['title'];
            $email=$row['email'];
            $phoneNo=$row['phoneNo'];
            $totalWLUs=$row['totalWLUs'];
        }
        echo"<div class='modal-dialog'>
                <div class='modal-content'>
                    <div class='modal-header'>
                        <button type='button' class='close' data-dismiss='modal' aria-hidden='true'>&times;</button>
                        <h4 class='modal-title' id='myModalLabel'>Upate Personal Information</h4>
                    </div>
                    <div class='modal-body'>
                        <form class='form-horizontal' role='form'>
                            <div class='form-group'>
                                <label  class='col-sm-3 control-label home-label'>First Name</label>
                                <div class='col-sm-9'>
                                    <input type='text' class='form-control' id='first_name' value='$firstname'></input>
                                </div>
                            </div>
                            <div class='form-group'>
                                <label  class='col-sm-3 control-label home-label'>Last Name</label>
                                <div class='col-sm-9'>
                                    <input type='text' class='form-control' id='last_name' value='$lastname'></input>
                                </div>
                            </div>
                            <div class='form-group'>
                                <label  class='col-sm-3 control-label home-label'>Title</label>
                                <div class='col-sm-9'>
                                    <select class='form-control' id='title' value='$title'>";
                                    titleSelect($title);
                                    echo"</select>
                                </div>
                            </div>
                            <div class='form-group'>
                                <label  class='col-sm-3 control-label home-label'>Email</label>
                                <div class='col-sm-9'>
                                    <input type='email' class='form-control' id='email' value='$email'></input>
                                </div>
                            </div>
                            <div class='form-group'>
                                <label  class='col-sm-3 control-label home-label'>Phone Number</label>
                                <div class='col-sm-9'>
                                    <input type='number' class='form-control' id='phoneNo' value='$phoneNo'></input>
                                </div>
                            </div>
                        </form>";
                    
                echo"</div>   
                    <div class='modal-footer'>                   
                    <button id='m_pinfo_button'  class='btn btn-primary' data-dismiss='modal'>Submit</button>
                    </div>    
                </div>
             </div>";
    }
    function model($username,$type){
         echo"<div class='modal-dialog'>
                <div class='modal-content'>
                    <div class='modal-header'>
                    <button type='button' class='close' data-dismiss='modal' aria-hidden='true'>&times;</button>";   
            if($type=='M'){
                echo"<h4 class='modal-title' id='myModalLabel'>Upate Task Information</h4>
                    </div>
                    <div class='modal-body'>";
                modifyStaff($username);
                echo"</div>   
                    <div class='modal-footer'>                   
                    <button id='m_sub_button'  class='btn btn-primary' data-id='$username' data-dismiss='modal'>Submit</button>
                    </div>    
                </div>
             </div>";
            }                    
            else if($type=='D'){
                echo"<h4 class='modal-title' id='myModalLabel'>Confirm Delete</h4>
                    </div>";
                deleteStaffshow($username);
            }
            else if($type=='V'){
                echo"<h4 class='modal-title' id='myModalLabel'>View Tasks</h4>
                    </div>
                    <div class='modal-body'>";
                viewTasks($username);
                echo"</div>   
                    <div class='modal-footer'>                   
                    <button type='button' class='btn btn-default' data-dismiss='modal'>Close</button>
                    </div>    
                </div>
             </div>";
            }
                                
               
    }
    function deleteStaffshow($staffname){
        include 'sqlconnect.php';
        $sql="SELECT *FROM staff WHERE username='$staffname'";
        $result=mysqli_query($conn,$sql);
        while($row=mysqli_fetch_array($result)){
            $firstname=$row['firstName'];
            $lastname=$row['lastName'];
        }
        $sql="SELECT count(1) as num FROM allocated_task WHERE username='$staffname'";
        $result=mysqli_query($conn,$sql);
        while($row=mysqli_fetch_array($result)){
            $num=$row['num'];
        }
        if($num>0){
            echo"<div class='modal-body'>
                    <h4>The $firstname $lastname's message has already used</h4>
                </div>   
                    <div class='modal-footer'>                   
                    <button type='button' class='btn btn-default' data-dismiss='modal'>Close</button>
                    </div>    
                </div>
             </div>";
        }
        else{
            echo"<div class='modal-body'>
                    <h4>Are you sure you want to delete $firstname $lastname's all data</h4>
                    <h5 class='text-danger'>The deleted data can't be recovered!!!</h5>
                </div>   
                    <div class='modal-footer'>                   
                    <button id='delete_staff' type='button' class='btn btn-danger' data-dismiss='modal' data-name='$staffname'>DELETE</button>
                    </div>    
                </div>
             </div>";
        }
    }
    function positionSelect($positionid){
        if($positionid==1){
            echo"<option value='1' selected='selected'>Head of Department</option>
                 <option value='2'>Secretory</option>
                 <option value='3'>Academic Staff</option>";
        }
        else if($positionid==2){
            echo"<option value='1'>Head of Department</option>
                 <option value='2' selected='selected'>Secretory</option>
                 <option value='3'>Academic Staff</option>";
        }
        else if($positionid==3){
            echo"<option value='1'>Head of Department</option>
                 <option value='2'>Secretory</option>
                 <option value='3' selected='selected'>Academic Staff</option>";
        }
    }
    function titleSelect($title){
        if($title=='Mr'){
            echo"<option value='Mr' selected='selected'>Mr</option>
                 <option value='Prof'>Prof</option>
                 <option value='Miss'>Miss</option>";
        }
        else if($title=='Prof'){
            echo"<option value='Mr'>Mr</option>
                 <option value='Prof' selected='selected'>Prof</option>
                 <option value='Miss'>Miss</option>";
        }
        else if($title=='Miss'){
            echo"<option value='Mr'>Mr</option>
                 <option value='Prof'>Prof</option>
                 <option value='Miss' selected='selected'>Miss</option>";
        }
    }
    function modifyStaff($staffname){
        include 'sqlconnect.php';
        echo"<form id='update' method='POST' action='#'>
                <input type='text' id='type' value='M' style='display:none;'>
                <input type='text' id='staffname' value='$staffname' style='display:none;'>
                <div class='table-responsive'>
                    <table class='table table-striped home-task-table'>
                        <tbody>";
        $sql="SELECT *FROM staff,position WHERE staff.idposition=position.idposition and username='$staffname'";
        $result=mysqli_query($conn,$sql);
        while($row=mysqli_fetch_array($result)){
            $positionid=$row['idposition'];
            $title=$row['title'];
             echo"<tr>
                    <td class='td-label'>Title:</td>
                    <td><select class='form-control' id='title'>";
                        titleSelect($title);
                    echo"</select></td>
                </tr>
                <tr>
                    <td class='td-label'>FirstName:</td>
                    <td><input type='text' class='form-control' id='firstname' value=\"".$row['firstName']."\"></td>
                </tr>
                <tr>
                    <td class='td-label'>LastName:</td>
                    <td><input type='text' class='form-control' id='lastname' value=\"".$row['lastName']."\"></td>
                </tr>
                <tr>
                    <td class='td-label'>Email:</td>
                    <td><input type='email' class='form-control' id='email' value=\"".$row['email']."\"></td>
                </tr>
                <tr>
                    <td class='td-label'>Work Phone:</td>
                    <td><input type='number' class='form-control' id='phoneNo' value=\"".$row['phoneNo']."\"></td>
                </tr>
                <tr>
                    <td class='td-label'>Position:</td>
                    <td><select class='form-control' id='position'>";
                        positionSelect($positionid);
                    echo"</select>
                    </td>
                </tr>"
                ;
        }
        echo"</tbody>
            </table>
            </div>
            </form>";
    }
    function viewTasks($staffname){
        include 'sqlconnect.php';
        $sql="SELECT DISTINCT task.taskID,actualWLUs,taskName,isFinished FROM task,task_hierarchy,allocated_task 
        WHERE task.taskID=allocated_task.taskID and username='$staffname' and task.taskID
        not in (SELECT subTaskID FROM workload.task_hierarchy)";
        $result=mysqli_query($conn,$sql);
        echo"<div class='container-fluid'>
        <div class='container-fluid template-list-item template-list-header'>
            <div class='col-md-7'>Task Name</div>       
            <div class='col-md-2'>WLUs</div>
            <div class='col-md-2'>Status</div>
        </div>";
        while($row=mysqli_fetch_array($result)){
            $taskname=$row['taskName'];
            $taskid=$row['taskID'];
            $wlu=$row['actualWLUs'];
            $status=$row['isFinished']==0?'unFinished':'Finished';
            $levelNum=1;
            echo"<div class='container-fluid template-list-item template-level-$levelNum' >
                    <div class='col-md-7'>$taskname</div>
                    <div class='col-md-2'>$wlu</div>
                    <div class='col-md-2'>$status</div>";
                    
            $t=mysqli_query($conn,'SELECT count(1) AS num FROM task_hierarchy WHERE taskID='.$taskid);
            while($row1=mysqli_fetch_array($t)){
            $num=$row1['num'];
            }
            if($num>0){
                echo"<div class='col-md-1 '><span class='glyphicon glyphicon-triangle-bottom subtemplete' data-id='$taskid'></span></div>    
                    </div>
                <div class='$taskid'>";
           	        getsubtask($taskid,$levelNum);
                echo"</div>
                </div>";
            }
            else{
                echo"</div>";
            }    
        }
    }
    function getsubtask($taskid,$levelNum){
        include 'sqlconnect.php';
            $sql="SELECT task_hierarchy.subtaskID,taskName,actualWLUs,isFinished FROM task_hierarchy,task,allocated_task 
            WHERE task.taskID=task_hierarchy.subTaskID and task_hierarchy.taskID=$taskid and allocated_task.taskID=task.taskID";
            $result=mysqli_query($conn,$sql);
            $levelNum++;
            while($row=mysqli_fetch_array($result)){
                $subtaskID=$row['subtaskID'];
                $taskname=$row['taskName'];
                $wlu=$row['actualWLUs'];
                $status=$row['isFinished']==0?'unFinished':'Finished';
                
                echo"<div class='container-fluid template-list-item template-level-$levelNum' >
                    <div class='col-md-7'>$taskname</div>
                    <div class='col-md-2'>$wlu</div>
                    <div class='col-md-2'>$status</div>";
                $t=mysqli_query($conn,'SELECT count(1) AS num FROM task_hierarchy WHERE taskID='.$subtaskID);
                while($row1=mysqli_fetch_array($t)){
                    $num=$row1['num'];
                }
                if($num>0){
                    echo"<div class='col-md-1 '><span class='glyphicon glyphicon-triangle-bottom subtemplete' data-id='$subtaskID'></span></div>    
                    </div>
                    <div class='$subtaskID'>";
           	            getsubtask($subtaskID,$levelNum);
                    echo"</div>";
                }
                else{
                    echo"</div>";
                }
            }
    }
    function getinfor($sql){
        $idposition=$_SESSION['idposition'];
        include 'sqlconnect.php';
        $resutl=mysqli_query($conn,$sql);
        while($row=mysqli_fetch_array($resutl)){
            $username=$row['username'];
            $title=$row['title']." ".$row['firstName']." ".$row['lastName'];
            if($row['idposition']==1){
                $position='Head of Department';
            }
            else if($row['idposition']==2){
                $position='Secretory';
            }
            else if($row['idposition']==3){
                $position='Academic Staff';
            }
            $email=$row['email'];
            $phone=$row['phoneNo'];
            $totalWLUs=$row['totalWLUs'];
            echo"<div class='staff-list' id='$username'>
                <p>
                    <div class='staff-list-button-group'>";
            if($idposition==1||$idposition==2){
                echo"<p><button class='btn btn-default st_button' data-username='$username' data-type='M'>Modify</button></p>
                        <p><button class='btn btn-default st_button' data-username='$username' data-type='D'>Delete</button></p>
                        <p><button class='btn btn-default st_button' data-username='$username' data-type='V'>View tasks</button></p>";
            }else{
                echo"<p><button class='btn btn-default st_button' data-username='$username' data-type='V'>View tasks</button></p>";
            }
                        
                echo"</div>
                <b>$title</b><br/>
                <b>Position: </b>$position<br/>
                <b>Email: </b>$email<br/>
                <b>Work Phone: </b>$phone<br/>
                <b>Total WLUs: </b>$totalWLUs/1600
                </p>
                <br/>
            </div>";
        }
    }
    function personal_infor_show(){
        include 'sqlconnect.php';
        $staffname=$_SESSION['username'];
        $sql="SELECT * FROM staff WHERE username='$staffname'";
        $result=mysqli_query($conn,$sql);
        while ($row=mysqli_fetch_array($result)){
            $firstname=$row['firstName'];
            $lastname=$row['lastName'];
            $title=$row['title'];
            $email=$row['email'];
            $phoneNo=$row['phoneNo'];
            $totalWLUs=$row['totalWLUs'];

            echo"<div class='col-md-3 col-md-offset-3 personal-information'>
                    <p class='text-left personal-information-item'>First Name: $firstname</p>
                    <p class='text-left personal-information-item'>Last Name: $lastname</p>
                    <p class='text-left personal-information-item'>Title: $title</p>
                    <p class='text-left personal-information-item'>Email: $email</p>
                    <p class='text-left personal-information-item'>Phone Number: $phoneNo</p>
                </div>
                <div class='col-md-3 col-md-offset-1 personal-information'>
                    <p class='text-left personal-information-item'>Total Work Hours: $totalWLUs</p>           
                </div>";
        }      
    }

?>

    